(function($){
    //使对象产生hover事件
    $.fn.hoverClass = function(b) {
        var a = this;
        a.each(function(c) {
            a.eq(c).hover(function() {
                $(this).addClass(b)
            },
            function() {
                $(this).removeClass(b)
            });
        });
        return a;
    };
    //输入框占位符效果
    $.fn.focusText = function(c) {
        var a = this;
        if(typeof(c) === "boolean" && !c){ //可以传入false取消占位符
            a.unbind("focus",_focus);
            a.unbind("blur",_blur);
            return;
        }
        var b = (c == null) ? $(a).val() : c;
        a.val(b);
        a.bind("focus",{"target":a,"placeholder":b},_focus);
        a.bind("blur",{"target":a,"placeholder":b},_blur);
        function _focus(event){
            var tar = event.data.target;
            var text = event.data.placeholder;
            if (tar.val() === text) {
                tar.val("")
            }
        }
        function _blur(event){
            var tar = event.data.target;
            var text = event.data.placeholder;
            if (tar.val() === "") {
                tar.val(text);
            }
        }
        return a;
    };
    //输入框焦点样式
    $.fn.focusChangeStyle = function(b) {
        var a = this;
        var b = (b == null) ? "it_focus": b;
        a.focus(function() {
            $(this).addClass(b)
        });
        a.blur(function() {
            $(this).removeClass(b)
        });
        return a
    };
    //设置图片统一大小
    $.fn.autoImgW = function(d) {
        var a = this;
        var b = (d == null) ? a.width() : d;
        var c = a.find("img");
        c.each(function(f) {
            var e = $(this).width();
            var g = $(this).height();
            var h = $(this).attr("src");
            if (e >= b) {
                $(this).css({
                    width: b,
                    height: g * (b / e)
                }).wrap("<a href=" + h + " target='_blank' title='点击在新窗口查看大图'></a>")
            }
        });
        return a;
    };
    //设置下拉跳转链接
    $.fn.openSelVal = function(b) {
        var a = this;
        a.change(function() {
            if (a.val() !== 0) {
                if (b === "self") {
                    window.location = a.val()
                } else {
                    window.open(a.val())
                }
            }
        });
        return a
    };
    //类似tab切换效果
    $.fn.overOnlyClass = function(b) {
        var a = this;
        a.each(function(c) {
            a.eq(c).mouseenter(function() {
                a.removeClass(b);
                $(this).addClass(b)
            })
        });
        return a
    };
    
    //广告切换插件 by yinlong
    $.fn.soChange = function(b) {
        b = $.extend({
            botPrev:null,//按钮上一个
			botNext:null,//按钮下一个
            changeType:'',//切换方式，可选：fade,slide，默认为fade
            slideTime:1000,//平滑过渡时间，默认为1000ms，为0或负值时，忽略changeType方式，切换效果为直接显示隐藏
            thumbObj: null, //导航元素
            thumbNowClass: "now", //导航当前项样式
            changeClass: null, //轮播主体如果以改变样方式显示请设置此项
            thumbOverEvent: true,//导航元素是否直接mouseover动作
            autoChange: true, //是否自动开始轮播
            clickFalse: true, //禁止导航点击冒泡
            overStop: true, //鼠标在导航时是否停止所有
            changeTime: 5000, //轮播间隔时间
            delayTime: 400, //导航上鼠标事件延迟时间
            start:1 //初始项
        },
        b || {});
        var h = this;
        var i;
        var k = h.length;
        var e = 0;
        var g = 0;
        var c = [];
        var f;
        var hasthumb;
        var thumb;
        
        if(b.thumbObj != null){
            hasthumb = true;
            thumb = $(b.thumbObj);
        }else{
            hasthumb = false;
        }
        
        if($(h).size()<=1){ //只有一张图或没有图，就不要显示导航和执行轮播了
            if (hasthumb) {
                thumb.remove();
            }
            return;
        }
        
        function d() {
            if (e != g) {
                
                if (hasthumb) {
                    thumb.removeClass(b.thumbNowClass).eq(g).addClass(b.thumbNowClass)
                }
                
                if (b.changeClass != null) {
                    h.eq(e).removeClass(b.changeClass);
                    h.eq(g).addClass(b.changeClass)
                } else {
                        
                    if(b.changeType=='fade' && b.slideTime > 0){
                        h.eq(g).fadeIn(b.slideTime);
                        h.eq(e).fadeOut(b.slideTime);
                    }else{
                        h.eq(g).css("visibility", "visible");
                        h.eq(e).css("visibility", "hidden")
                    }
                }
                e = g;
                if (b.autoChange == true) {
                    stopAll();
                    start();
                }
            }
        }
        function stopAll() {
            for (var i = 0; i < c.length; i++) {
                clearInterval(c[i])
            }
        }
        function start() {
            c.push(setInterval(j, b.changeTime))
        }
        function j() {
            g = (e + 1) % k;
            d();
        }
        
        //点击上一个
		if (b.botNext!=null) {
			$(b.botNext).click(function () {
				if(h.queue().length<1){
				runNext();}
				return false;
			});
		}

        //点击下一个
		if (b.botPrev!=null) {
			$(b.botPrev).click(function () {
				if(h.queue().length<1){
				g = (e + 1) % k;
				d();}
				return false;
		});
		}
        
        if (hasthumb) {
            if(thumb.index($("."+b.thumbNowClass)[0]) !== -1 ){ //如果设定首显项
                g = thumb.index($("."+b.thumbNowClass)[0]);
            }else{
                g = b.start-1; //没有首显项就使用配置信息的值
                thumb.removeClass(b.thumbNowClass).eq(0).addClass(b.thumbNowClass);
            }
            
            thumb.click(function() {
                g = thumb.index($(this));
                d();
                if (b.clickFalse == true) {
                    return false
                }
            });
            
            if (b.thumbOverEvent == true) {
                thumb.hover(function() {
                    g = thumb.index($(this));
                    f = setTimeout(function() {
                        d();
                        stopAll()
                    },
                    b.delayTime);
                    return false
                },
                function() {
                    clearTimeout(f);
                    return false
                })
            }
        }
        
        if (b.changeClass != null) {
            h.removeClass(b.changeClass).eq(g).addClass(b.changeClass)
        } else {
            if(b.changeType=='fade' && b.slideTime > 0){
                h.css("display", "none").eq(g).css("display", "block")
            }else{
                h.css("visibility", "hidden").eq(g).css("visibility", "visible")
            }
        }
        
        if (b.autoChange == true) {
            c.push(setInterval(j, b.changeTime));
            if (b.overStop == true) {
                h.mouseover(function() {
                    stopAll();
                    return false
                }).mouseout(function() {
                    start();
                    return false
                })
            }
        }
        
        d(); //初始化时就执行一下轮播
        
        this.stop = stopAll;
        this.start = start;
        h.bind("soChange",
        function(event, i) {
            g = i;
            d()
        });
        return this
    }
})(jQuery);

var userAgent = navigator.userAgent.toLowerCase();
var is_opera = userAgent.indexOf('opera') != -1 && opera.version();
var is_moz = (navigator.product == 'Gecko') && userAgent.substr(userAgent.indexOf('firefox') + 8, 3);
var is_ie = (userAgent.indexOf('msie') != -1 && !is_opera) && userAgent.substr(userAgent.indexOf('msie') + 5, 3);
var childWindow;
function bytes(str){
    var len=0;
    for(var i=0;i<str.length;i++){
        if(str.charCodeAt(i)>127){
            len++;
        }
        len++;
    }
    return len;
}

 
String.prototype.replaceAll = function(s1,s2) {
    return this.replace(new RegExp(s1,"igm"),s2);
}

function updatecode() {
    var img = "index.php?user/code/"+Math.random();
    $('#verifycode').attr("src",img);
}

$.dialog.setConfig('base', 'css/common/dialog');


function ask_submit(){
    document.searchform.action='index.php?question/add'+g_suffix;
    document.searchform.submit();
}

function search_submit(){
    word=encodeURI($('#kw').val());
    document.searchform.action='index.php?question/search/3/'+word+g_suffix;
    document.searchform.submit();
}
function hot_search(index){
    var hot = $("#hot"+index).text();
    $("#kw").val(hot);
    search_submit();
}

var copy2Clipboard=function(txt){
    if(window.clipboardData){
        window.clipboardData.clearData();
        window.clipboardData.setData("Text",txt);
    }
    else if(navigator.userAgent.indexOf("Opera")!=-1){
        window.location=txt;
    }
    else if(window.netscape){
        try{
            netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
        }
        catch(e){
            alert("您的firefox安全限制限制您进行剪贴板操作，请打开’about:config’将signed.applets.codebase_principal_support’设置为true’之后重试，相对路径为firefox根目录/greprefs/all.js");
            return false;
        }
        var clip=Components.classes['@mozilla.org/widget/clipboard;1'].createInstance(Components.interfaces.nsIClipboard);
        if(!clip)return;
        var trans=Components.classes['@mozilla.org/widget/transferable;1'].createInstance(Components.interfaces.nsITransferable);
        if(!trans)return;
        trans.addDataFlavor('text/unicode');
        var str=new Object();
        var len=new Object();
        var str=Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);
        var copytext=txt;
        str.data=copytext;
        trans.setTransferData("text/unicode",str,copytext.length*2);
        var clipid=Components.interfaces.nsIClipboard;
        if(!clip)return false;
        clip.setData(trans,null,clipid.kGlobalClipboard);
    }
}

function getcookie(name) {
    var cookie_start = document.cookie.indexOf(name);
    var cookie_end = document.cookie.indexOf(";", cookie_start);
    return cookie_start == -1 ? '' : unescape(document.cookie.substring(cookie_start + name.length + 1, (cookie_end > cookie_start ? cookie_end : document.cookie.length)));
}

function setcookie(cookieName, cookieValue, seconds, path, domain, secure) {
    seconds = seconds ? seconds : 8400000;
    var expires = new Date();
    expires.setTime(expires.getTime() + seconds);
    document.cookie = escape(cookieName) + '=' + escape(cookieValue)
    + (expires ? '; expires=' + expires.toGMTString() : '')
    + (path ? '; path=' + path : '/')
    + (domain ? '; domain=' + domain : '')
    + (secure ? '; secure' : '');
}


function addfavorite(){
    if (document.all){
        window.external.addFavorite(g_site_url,document.title);
    }else if (window.sidebar){
        window.sidebar.addPanel(document.title,g_site_url, "");
    }
}

function SetHomepage(){
    if(document.all){
        document.body.style.behavior="url(#default#homepage)";
        document.body.setHomePage(g_site_url);
    }
    else if(window.sidebar){
        if(window.netscape){
            try{
                netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
            }
            catch(e){
                alert("您的浏览器未启用[设为首页]功能，开启方法：先在地址栏内输入about:config,然后将项 signed.applets.codebase_principal_support 值该为true即可");
            }
        }
        var prefs=Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
        prefs.setCharPref("browser.startup.homepage",g_site_url);
    }
}
function checkall(name) {
    var e = is_ie ? event : checkall.caller.arguments[0];
    obj = is_ie ? e.srcElement : e.target;
    var arr = document.getElementsByName(name);
    var k = arr.length;
    for(var i=0; i<k; i++) {
        arr[i].checked = obj.checked;
    }
}

function sendmsg(username){
	code_message = '';
    if(messcode==1){
        code_message='<tr><td align="right" width="100">验证码 ：</td><td width="90%"><input maxlength="4" class="input4" name="code" size="10" /><img align="absmiddle" id="verifycode" onclick="javascript:updatecode();" src="'+'index.php?user/code"/>&nbsp;<a href="javascript:updatecode();">看不清，换一个</a><span id="codetip"></span></td></tr>';
    }
    $.dialog({
        id:'sendmsgDiv',
        position:'center',
        align:'left',
        fixed:1,
        width:450,
        title:'发送消息',
        fnOk:function(){
            document.sendmsgForm.submit();
            $.dialog.close('sendmsgDiv')
        },
        fnCancel:function(){
            $.dialog.close('sendmsgDiv')
        },
        content:'<form name="sendmsgForm"  action="'+'index.php?message/send" method="post" ><input type="hidden" name="username" value="'+username+'"><table cellspacing="4" cellpadding="0" width="100%" border="0" valign="top"><tr><td class="f14" valign="top" nowrap align="left" width="10%" height="35">接收人 :&nbsp;&nbsp; </td><td valign="top" height="35">'+username+'</td></tr><tr><td class="f14" valign="top" nowrap align="left" height="35">主题 :&nbsp;&nbsp; </td><td valign="top" width="60%" height="35"><input type="text" class="input2" size="45"   name="title"  maxlength="20"></td></tr><tr><td class="f14" valign="top" nowrap align="left" height="35"> 内容 :&nbsp;&nbsp; </td><td valign="top" height="35"><textarea name="content" style="width: 95%; padding-top: 1px; font-size: 14px;" rows="8" ></textarea></td></tr>'+code_message+'</table></form>'
    });
}

function toQzoneLogin(){
    childWindow = window.open("index.php?user/qqlogin","TencentLogin","width=450,height=320,menubar=0,scrollbars=1, resizable=1,status=1,titlebar=0,toolbar=0,location=1");
} 
function closeChildWindow(){
    childWindow.close();
}

